package team.seekers.hula.user.userHula.service;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import team.seekers.hula.common.entity.PageResult;
import team.seekers.hula.question.answer.entity.dto.AnswerAddDTO;
import team.seekers.hula.question.answer.service.AnswerService;
import team.seekers.hula.question.point.entity.dto.PointAddDTO;
import team.seekers.hula.question.point.service.PointService;
import team.seekers.hula.question.question.entity.dto.QuestionAddDTO;
import team.seekers.hula.question.question.service.QuestionService;
import team.seekers.hula.user.user.entity.dto.UserHulaDTO;
import team.seekers.hula.user.user.service.UserService;
import team.seekers.hula.user.userHula.dao.UserHulaDao;
import team.seekers.hula.user.userHula.entity.dao.UserHulaDO;
import team.seekers.hula.user.userHula.entity.dto.UserHulaQueryDTO;
import team.seekers.hula.user.userHula.entity.vo.UserHulaInfoVO;
import team.seekers.hula.user.userHula.entity.vo.UserHulaListVO;
import team.seekers.hula.user.userHula.mapper.UserHulaMapper;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Map;

/**
 * 用户候浪者目标
 * @author hbc
 * @since 2023-01-30
 */
@Service
public class UserHulaService extends ServiceImpl<UserHulaMapper, UserHulaDO> {

    @Resource
    private UserHulaDao userHulaDao;

    @Resource
    private UserService userService;

    @Resource
    private QuestionService questionService;

    @Resource
    private PointService pointService;

    @Resource
    private AnswerService answerService;

    public UserHulaInfoVO getHulaInfoVO(String userId) {
        UserHulaDO userHulaDO = getById(userId);

        UserHulaInfoVO userHulaInfoVO = new UserHulaInfoVO();
        BeanUtils.copyProperties(userHulaDO, userHulaInfoVO);

        // 填充用户信息
        userHulaInfoVO.setUserInfo(userService.getInfoVO(userHulaDO.getId()));

        // 填充问答信息
        userHulaInfoVO.setQuestion(questionService.getQuestionTitle(userHulaDO.getQuestionId()));
        userHulaInfoVO.setPoint(pointService.getPointKeyword(userHulaDO.getPointId()));
        userHulaInfoVO.setAnswer(answerService.getAnswerContent(userHulaDO.getAnswerId()));

        return userHulaInfoVO;
    }

    public PageResult<UserHulaListVO> pageOrderTime(UserHulaQueryDTO queryDTO) {
        LambdaQueryChainWrapper<UserHulaDO> query = userHulaDao.lambdaQuery();
        Page<UserHulaDO> page = query.page(new Page<>(queryDTO.getPageNow(), queryDTO.getPageSize()));

        if(page.getPages() == 0){
            return new PageResult<>();
        }

        ArrayList<UserHulaListVO> userHulaListVOS = new ArrayList<>();
        for (UserHulaDO record : page.getRecords()) {
            UserHulaListVO userHulaListVO = new UserHulaListVO();
            BeanUtils.copyProperties(record, userHulaListVO);

            // 填充用户信息
            userHulaListVO.setUserInfo(userService.getInfoVO(record.getId()));

            // 填充问答信息
            userHulaListVO.setQuestion(questionService.getQuestionTitle(record.getQuestionId()));
            userHulaListVO.setPoint(pointService.getPointKeyword(record.getPointId()));
            userHulaListVO.setAnswer(answerService.getAnswerContent(record.getAnswerId()));

            userHulaListVOS.add(userHulaListVO);
        }

        return new PageResult<>(page.getPages(), userHulaListVOS);

    }

    @Transactional(rollbackFor = Exception.class)
    public void createHular(UserHulaDTO userHulaDTO) {
        UserHulaDO userHulaDO = new UserHulaDO();
        BeanUtils.copyProperties(userHulaDTO,userHulaDO);

        // 填充问题
        ArrayList<PointAddDTO> pointAddDTOS = new ArrayList<>();
        pointAddDTOS.add(PointAddDTO.builder()
                .answerAddDTO(AnswerAddDTO.builder()
                        .content(userHulaDTO.getAnswer())
                        .visibility(2)
                        .build())
                .keyword(userHulaDTO.getPoint())
                .build());


        Map<String, String> map = questionService.add(QuestionAddDTO.builder()
                .userId(userHulaDTO.getId())
                .title(userHulaDTO.getQuestion()).pointAddDTOList(pointAddDTOS)
                .context(userHulaDTO.getContext())
                .build());

        userHulaDO.setQuestionId(map.get("question"));
        userHulaDO.setPointId(map.get("point"));
        userHulaDO.setAnswerId(map.get("answer"));

        save(userHulaDO);
    }
}
